//
//  FP_UserPriceInquiryInfoCell.m
//  FootPad
//
//  Created by 张浩 on 2022/2/17.
//  Copyright © 2022 胶享悦. All rights reserved.
//

#import "MM_CodeResultOperationRecordCell.h"

static const CGFloat addTitleTag = 200;

@interface MM_CodeResultOperationRecordCell ()

@property (nonatomic, strong) UIView *baseView;

@end

@implementation MM_CodeResultOperationRecordCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        [self.contentView addSubview:_baseView];
        
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.right.top.bottom.equalTo(self.contentView);
            make.height.equalTo(90);
        }];
        
    }
    return self;
}

- (void)setModel:(MM_CodeResultOperationRecordInfoModel *)model {
    _model = model;
    
    NSString *numberStr = [NSString stringWithFormat:@"%@\n%@",model.qty,model.after_change_qty.integerValue <0?@"":model.after_change_qty];
    
    if (model.job_type.integerValue == 5 || model.job_type.integerValue == 6) {
        numberStr = [NSString stringWithFormat:@"%@\n%@",model.job_type.integerValue == 5?[NSString stringWithFormat:@"+%@",model.qty]:[NSString stringWithFormat:@"-%@",model.qty],model.after_change_qty.integerValue <0?@"":model.after_change_qty];
    }
    
    NSString *remark = @"";
    
    if (model.files.count > 0) {
        remark = !stringIsEmpty(model.reason)?[NSString    stringWithFormat:@"%@\n[查看图片]",model.reason]:@"无[查看图片]";
    } else {
        remark = !stringIsEmpty(model.reason)?model.reason:@"无";
    }
    
    NSArray *titleArr = @[[NSString stringWithFormat:@"%@\n%@",[self setUpJobTypeDescStr:model.job_type],model.change_options],[NSString stringWithFormat:@"%@\n%@\n%@",model.warehouse_name,model.warehouse_area_property_str,model.location_name],stringIsEmpty(model.qty)?model.qty:numberStr,!stringIsEmpty(model.reason)?model.reason:@"无",[NSString stringWithFormat:@"%@\n%@",!stringIsEmpty(model.creator)?model.creator:@"",!stringIsEmpty(model.last_update_time)?model.last_update_time:@""]];
//    NSArray *titleWArr = @[@(9),@(7),@(7),@(5),@(5),@(6)];
    CGFloat titleW = ScreenW /5.0;
        
    for (UIView*subView in self.baseView.subviews) {
        
        [subView removeFromSuperview];
    }
    
    for (int i=0; i<titleArr.count; i++) {
        
        UIView *titleView = [[UIView alloc] init];
        titleView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        titleView.tag = addTitleTag + i;
        [self.baseView addSubview:titleView];
        
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(titleW *i);
            make.top.height.equalTo(self.baseView);
            make.width.equalTo(titleW);
        }];
                
        UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:titleArr[i] Font:FONT(12) TextColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
        nameLabel.numberOfLines = 0;
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.textColor = [ColorUtil colorWithHexadecimal:@"#333333"];
        
        if (i == 6) {
            
            nameLabel.textColor = MM_ColorUtil(@"#DE1212");
        }
        
        [titleView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
//            if (i == 0 || i== 1) {
//                make.left.equalTo(12);
//                make.right.equalTo(-5);
//            }else {
//                make.left.equalTo(10);
//            }
            
            if (i == titleArr.count - 1 || i == titleArr.count - 2) {
                make.left.equalTo(12);
                make.right.equalTo(-5);
            }
            
            make.centerX.centerY.equalTo(titleView);
        }];
        
        if (i == 0) {
            
            nameLabel.attributedText = [Custom_ZHTool createAttributedStringWithString:[NSString stringWithFormat:@"%@\n%@",[self setUpJobTypeDescStr:model.job_type],model.change_options] key:[self setUpJobTypeDescStr:model.job_type] keyFont:FONT_BOLD(12) keyColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
        }
        
        if (i == titleArr.count - 1) {
            nameLabel.attributedText = [Custom_ZHTool createAttributedStringWithString:[NSString stringWithFormat:@"%@\n%@",!stringIsEmpty(model.creator)?model.creator:@"",!stringIsEmpty(model.last_update_time)?model.last_update_time:@""] key:model.creator keyFont:FONT_BOLD(12) keyColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
        }
        
        if (i == titleArr.count - 2) {
            
            nameLabel.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
            [nameLabel addGestureRecognizer:tap];
            
            nameLabel.attributedText = [Custom_ZHTool createAttributedStringWithString:remark key:@"[查看图片]" keyFont:FONT_BOLD(12) keyColor:[ColorUtil colorWithHexadecimal:@"#1F71C4"]];
        }
    }
}

- (void)tapClick {
    
    if (self.remarkBlock) {
        self.remarkBlock(self.model);
    }
}

- (NSString *)setUpJobTypeDescStr:(NSString *)jobType {
    
    NSString *type_str = @"未知";
    
    if (jobType.integerValue == 1) {
        type_str = @"盘点";
    } else if (jobType.integerValue == 2) {
        type_str = @"加工-结合";
    } else if (jobType.integerValue == 3) {
        type_str = @"加工-分割";
    } else if (jobType.integerValue == 4) {
        type_str = @"移动";
    } else if (jobType.integerValue == 5) {
        type_str = @"库存增加";
    } else if (jobType.integerValue == 6) {
        type_str = @"库存减少";
    }
    return type_str;
}

- (void)nameDataTapClick:(UITapGestureRecognizer *)tap {
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
